<?php
/**
 * Created by PhpStorm.
 * User: ysfl
 * Date: 16-3-5
 * Time: 下午2:14
 */

namespace modules\gwbn;

use Cross\MVC\Module;

class UserModule extends Module
{
    /**
     * 用户表
     *
     * @var string
     */
    protected $install = 'gwbn_install';
    protected $not_install = 'gwbn_not_install';
    protected $not_install_list = 'gwbn_not_install_list';

    function Install_Query()
    {
        return $this->link->getAll($this->install, '*');
    }

    function Install_Add($data)
    {
        #print_r($data);exit;
        //检查表单是否为空
        foreach ($data as $key => & $val) {

            if (empty($val)) {

                switch($key)
                {
                    case 'time':
                        $name = "时间";
                        break;
                    case 'account':
                        $name = "账号";
                        break;
                    case 'type':
                        $name = "资费";
                        break;
                    case 'mobile':
                        $name = "客户手机";
                        break;
                    case 'name':
                        $name = "客户名字";
                        break;
                    case 'speed':
                        $name = "速率";
                        break;
                    case 'price':
                        $name = '金额';
                        break;
                    case 'otime':
                        $name = "到期时间";
                        break;
                    case 'addr':
                        $name = "社区名字";
                        break;
                    case 'address':
                        $name = "详细地址";
                        break;
                    case 'source':
                        $name = "客户来源";
                        break;
                    case 'uname':
                        $name = "销售";
                        break;
                    default:
                        $name = "未知错误";
                        break;
                }

                //排除列表
                if (
                    $key != "domybox" &&
                    $key != "remarks"
                ){

                    return "请输入：".$name;

                    unset($data[$key]);
                }
            }
        }

        //检测是否已经存在
        $ck = $this->link->select($this->install,'account','account = $data["account"]');

        if($ck === $data['account']){
            return $ck;
        }

        //数据写入数据库
        if ($this->link->add($this->install, $data)){
            return "添加成功！";
        }
        else{
            return "添加失败，请联系管理员";
        }
    }

    function NotINstall()
    {

    }

    function Not_Install_Add($data)
    {
        if ($data['mobile'] == NULL){
            echo "手机号码没有填写";
            exit;
        }

        if($this->link->add($this->not_install,array(
            'time'      => $data['time'],
            'mobile'    => $data['mobile'],
            'name'      => $data['name'],
            'speed'     => $data['speed'],
            'content'   => $data['content'],
            'addr'      => $data['addr'],
            'address'   => $data['address'],
            'uname'     => $data['uname'],
        ))){
            echo "添加成功!";
        }
        else{
            echo "添加失败!";
        }
    }

    function Not_Install_Query()
    {
        return $this->link->getAll($this->not_install, '*');
    }

    function Not_Install_Query_List($data)
    {
        return $this->link->getAll($this->not_install_list, '*',array('id' => $data));
    }

    function Not_Install_Query_List_Num($data)
    {
        $e = $this->link->select('count(*)')
            ->from($this->not_install_list)
            ->where(array('id' => $data))
            ->stmt()
            ->fetchAll(\PDO::FETCH_ASSOC);

        return $e[0]['count(*)'];
    }

    function Not_Install_Set($data)
    {
        switch ($data['start']){

            case "ok":

                $sql = $this->link->update($this->not_install, array(
                    'start' => 1,
                ), array('entry' => $data['id']));

                $s = "确定成功！";

                break;

            case "set":

                if ($data['content'] == NULL){
                    echo "内容没有填写";
                    exit;
                }

                $sql = $this->link->add($this->not_install_list, array(
                    'id' => $data['id'],
                    'content' => $data['content'],
                    'time' => date("Y-m-d H:i:s"),
                    'name' => $_SESSION['uname']
                ));

                $s = "更新成功！";

                break;

            case "del":

                $sql = $this->link->update($this->not_install, array(
                    'start' => 2,
                ), array('entry' => $data['id']));

                $s = "成功删除！";

                break;

            default :

                $s = "错误的值";

                break;
        }

        if ($sql){
            return $s;
        }
    }
}